﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
//using IniTechCaash.Domain.EntityFramework;
//using System.IO;
//using IniTechCaash.Domain.Entities;


//namespace IniTechCaash.Domain.Services
//{
//    /// <summary>
//    /// Class to aid in interacting with file data associated with <see cref="SourceDocument"/>s. 
//    /// </summary>
//    public class SourceDocumentService : Service
//    {
//        public const string PATH = "Files\\";

//        public SourceDocumentService(CaashContext context)
//            : base(context)
//        {
//        }

//        /// <summary>
//        /// Removes a <see cref="SourceDocument"/> record from the database, and deletes the file that it represented. 
//        /// </summary>
//        /// <param name="sourceDocumentId">Id of the <see cref="SourceDocument"/> to delete</param>
//        /// <returns>true if the operation was a success, otherwise false</returns>
//        public bool DeleteSourceDocument(int sourceDocumentId)
//        {
//            try
//            {
//                _context.SourceDocuments.Remove(new SourceDocument { Id = sourceDocumentId, });
//                File.Delete(PATH + sourceDocumentId);
//            }
//            catch (Exception e)
//            {
//                return false;
//            }

//            return true;
//        }

//        /// <summary>
//        /// Gets a stored <see cref="SourceDocument"/> and attatches and opens it's associated <see cref="File"/>. 
//        /// </summary>
//        /// <param name="sourceDocuemntId">Id of the <see cref="SourceDocument"/></param>
//        /// <returns>a <see cref="SourceDocument"/> with it's associated <see cref="File"/> or null if not found</returns>
//        public SourceDocument GetSourceDocument(int sourceDocumentId)
//        {
//            SourceDocument sourceDocument = _context.SourceDocuments.FirstOrDefault(sd => sd.Id == sourceDocumentId);
//            if (sourceDocument == null)
//                return null;

//            sourceDocument.File = File.OpenRead(PATH + sourceDocument.Id);

//            return sourceDocument;
//        }

//        /// <summary>
//        /// Saves a new <see cref="SourceDocument"/> and copys its associated File. 
//        /// </summary>
//        /// <param name="sourceDocument"><see cref="SourceDocument"/> to save</param>
//        /// <returns>true if the operation was a success, otherwise false</returns>
//        public bool SaveSourceDocument(SourceDocument sourceDocument)
//        {
//            if (sourceDocument.File == null)
//                return false;

//            bool entitySaved = false;
//            try
//            {
//                _context.SourceDocuments.Add(sourceDocument);
//                _context.SaveChanges();
//                entitySaved = true;

//                FileStream ourFile = File.OpenWrite(PATH + sourceDocument.Id);
//                sourceDocument.File.CopyTo(ourFile);
//                ourFile.Close();
//            }
//            catch (Exception e)
//            {
//                if (entitySaved)
//                {
//                    _context.SourceDocuments.Remove(sourceDocument);
//                    _context.Entry<SourceDocument>(sourceDocument).State = System.Data.Entity.EntityState.Deleted;
//                    _context.SaveChanges();
//                }
//                return false;
//            }

//            return true;
//        }
//    }
//}
